package com.example.demo.src;

/**
 * Created by Easettle
 * Date:2021-01-03 13:49
 * Email:easettle@qq.com
 */
public abstract class Handler<T> {
    protected Handler chain;

    /**
     * 不使用public的原因是，使Handler类更加高聚合
     * @param handler
     */
    private void next(Handler handler) {
        this.chain = handler;
    }

    /**
     * 判断处理
     *
     * @param member
     */
    public abstract void doHandler(Member member);

    /**
     * 因为建造者模式要构建的是节点处理者，所以我们把Builder作为Handler的静态类部内，并且客户端不需要进行任何的链式组装
     * @param <T>
     */
    public static class Builder<T> {
        private Handler<T> handler;
        private Handler<T> tail;

        public Builder<T> addHandler(Handler<T> handler) {
            if (this.handler == null) {
                this.handler = this.tail = handler;
                return this;
            }
            this.tail.next(handler);
            this.tail = handler;
            return this;
        }

        public Handler<T> build() {
            return this.handler;
        }
    }
}
